package com.bzkj.msfx.service.impl;


import com.alibaba.fastjson2.JSONObject;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.msfx.domain.Logistics;
import com.bzkj.msfx.mapper.LogisticsMapper;
import com.bzkj.msfx.service.LogisticsService;
import com.bzkj.msfx.utils.FusionHttpUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @projectName: btyy
 * @package: com.bzkj.btyy.service.impl.msfx
 * @className: LogisticsServiceImpl
 * @description: 配送单位管理服务层实现类
 * @author: C1309
 * @createDate: 2024年03月27日
 * @version: v1.0
 */
@Service
public class LogisticsServiceImpl implements LogisticsService {

    @Resource
    private FusionHttpUtils fusionHttpUtils;

    @Resource
    private LogisticsMapper logisticsMapper;

    @Value("${pk_org.code}")
    private String pkOrgCode;


    /**
     * 查询配送单位信息
     *
     * @param logistics 查询条件
     * @return 配送单位信息列表
     */
    @Override
    public TableDataInfo queryLogistics(Logistics logistics) {
        return fusionHttpUtils.getTableDataInfo("/basic/logistics/list", logistics);
    }

    /**
     * 新增配送单位信息
     *
     * @param logistics 配送单位信息
     * @return 新增结果
     */
    @Override
    public AjaxResult addLogistics(Logistics logistics) {
        try {
            AjaxResult ajaxResult = fusionHttpUtils.sendPostRequest("/basic/logistics", null, JSONObject.toJSONString(logistics), "application/json");
            if (ajaxResult.isSuccess()) {
                logistics.setBusinessEntId(pkOrgCode);
                logistics.setId(ajaxResult.get("data").toString());
                logisticsMapper.addLogistics(logistics);
            }
            return ajaxResult;
        } catch (IOException e) {
            return AjaxResult.error("插入配送单位信息失败，具体原因：" + e.getMessage());
        }
    }

    /**
     * 删除配送单位信息
     *
     * @param id 配送单位id
     * @return 删除结果
     */
    @Override
    public AjaxResult deleteLogistics(String id) {
        try {
            AjaxResult ajaxResult = fusionHttpUtils.sendDeleteRequest("/basic/logistics/" + id, null, null, null);
            if (ajaxResult.isSuccess()) {
                logisticsMapper.deleteLogistics(id);
            }
            return ajaxResult;
        } catch (IOException e) {
            return AjaxResult.error("删除配送单位信息失败，具体原因：" + e.getMessage());
        }
    }

    /**
     * 查询码上放心往来单位信息
     *
     * @return 查询结果
     */
    @Override
    public AjaxResult queryMsfxCustomerInfo() {
        try {
            return fusionHttpUtils.sendGet("/basic/logistics/msfx/", null);
        } catch (RuntimeException e) {
            return AjaxResult.error("查询码上放心往来单位信息失败，具体原因：" + e.getMessage());
        }
    }
}
